﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;
using System.ComponentModel;
using System.Web;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.Office.Server;
using Microsoft.Office.Server.UserProfiles;
using Microsoft.SharePoint.Taxonomy;
using System.Collections.Specialized;

namespace wpKorabliksColleagues.VisualWebPart1
{
    public partial class VisualWebPart1UserControl : UserControl
    {

        #region Members
        public bool CustomCheckboxProp { get; set; } 
        #endregion


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadManagedMetaDataInformation();
            }


        }

        protected void __Button1OnClick(Object sender, EventArgs e)
        {
            if(!string.IsNullOrEmpty(TextBox2.Text))
                LoadSelectedUsers(0,TextBox2.Text);
        }

        protected void __OnSelectedNodeChanged(Object sender, EventArgs e)
        {
            LoadSelectedUsers(1,((TreeView)sender).SelectedValue.ToString());
            TextBox2.Text = string.Empty;
        }

        private void LoadSelectedUsers(int who, string TreeSelNode)
        {
            string sURL = "http://localhost";
            string searchWay = string.Empty;
            List<dsData> dsProperty = new List<dsData>();
            SPSite site = new SPSite(sURL);
            SPServiceContext context = SPServiceContext.GetContext(site);
            UserProfileManager uprofileManager = new UserProfileManager(context);


            ProfileBase[] searchResults = uprofileManager.Search((who == 0) ? TextBox2.Text : theTree.SelectedValue.ToString());

                foreach (ProfileBase pb in searchResults)
                {
                    dsData dRow = new dsData();
                    if (pb.GetProfileValueCollection("PreferredName").Value != null)
                        dRow.FullName = pb.GetProfileValueCollection("PreferredName").Value.ToString();
                    if (pb.GetProfileValueCollection("SPS-JobTitle").Value != null)
                        dRow.Staff = pb.GetProfileValueCollection("SPS-JobTitle").Value.ToString();
                    if (pb.GetProfileValueCollection("PictureUrl").Value != null && pb.GetProfileValueCollection("PictureUrl").Value.ToString() != string.Empty)
                        dRow.ImageUrl = pb.GetProfileValueCollection("PictureUrl").Value.ToString();
                    else
                        dRow.ImageUrl = sURL + "/_layouts/photo/no-photo.png";
                    if (pb.GetProfileValueCollection("WorkPhone").Value != null)
                        dRow.Phone = pb.GetProfileValueCollection("WorkPhone").Value.ToString();
                    if (pb.GetProfileValueCollection("WorkEmail").Value != null)
                        dRow.Mail = pb.GetProfileValueCollection("WorkEmail").Value.ToString();
                    if (pb.GetProfileValueCollection("SPS-Birthday").Value != null)
                        dRow.Birthday = DateTime.Parse(pb.GetProfileValueCollection("SPS-Birthday").Value.ToString());
                    dsProperty.Add(dRow);
                }

            //--
            dsProperty.Sort((x, y) => DateTime.Compare(x.Birthday, y.Birthday));
            //--
            Table tb = new Table();
            TableRow tRow;
            TableCell tCell;

            foreach (dsData row in dsProperty)
            {
                //Label1.Text += "here--";
                //--
                tRow = new TableRow();
                tRow.BorderWidth = 0;
                //--
                tCell = new TableCell();
                tCell.Width = 90;

                Image im = new Image();
                im.BorderWidth = 1;
                im.Width = 80;
                im.ImageUrl = row.ImageUrl;

                tCell.Controls.Add(im);
                tRow.Cells.Add(tCell);
                //--
                tCell = new TableCell();
                //tCell.Width = 600;
                tCell.VerticalAlign = VerticalAlign.Top;
                tCell.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Left;
                //--
                if (row.SiteUrl != null)
                {
                    HyperLink hl1 = new HyperLink();
                    hl1.Text = row.FullName + "<br>";
                    hl1.Font.Size = 9;
                    hl1.Font.Bold = true;
                    hl1.Style.Add("color", "#3472C7");
                    hl1.NavigateUrl = row.SiteUrl;
                    tCell.Controls.Add(hl1);
                }
                else
                {
                    System.Web.UI.WebControls.Label l1 = new System.Web.UI.WebControls.Label();
                    l1.Style.Add("color", "#3472C7");
                    l1.Text = row.FullName + "<br>";
                    l1.Font.Size = 9;
                    l1.Font.Bold = true;
                    tCell.Controls.Add(l1);
                }
                //--
                if (row.Department != null)
                {
                    System.Web.UI.WebControls.Label l4 = new System.Web.UI.WebControls.Label();
                    l4.Text = row.Department + "<br>";
                    l4.Font.Size = 8;
                    tCell.Controls.Add(l4);
                }
                //--
                if (row.Staff != null)
                {
                    System.Web.UI.WebControls.Label l5 = new System.Web.UI.WebControls.Label();
                    l5.Text = row.Staff + "<br>";
                    l5.Font.Size = 8;
                    tCell.Controls.Add(l5);
                }
                //--
                if (row.Phone != null)
                {
                    System.Web.UI.WebControls.Label l6 = new System.Web.UI.WebControls.Label();
                    l6.Text = "Телефон: " + row.Phone + "<br>";
                    l6.Font.Size = 8;
                    tCell.Controls.Add(l6);
                }
                if (row.Mail != null)
                {
                    HyperLink l7 = new HyperLink();
                    l7.Text = row.Mail + "<br>";
                    l7.Style.Add("color", "#3472C7");
                    l7.Font.Size = 8;
                    l7.NavigateUrl = "mailto:" + row.Mail;
                    tCell.Controls.Add(l7);
                }
                if (row.Birthday != null)
                {
                    System.Web.UI.WebControls.Label l8 = new System.Web.UI.WebControls.Label();
                    if ((DateTime.Parse(row.Birthday.ToString())).Month == DateTime.Now.Month && (DateTime.Parse(row.Birthday.ToString())).Day == DateTime.Now.Day)
                    {
                        l8.Style.Add("color", "green");
                        l8.Style.Add("font-weight", "bold");
                    }
                    else
                        l8.Style.Add("color", "lightgreen");
                    l8.Font.Size = 8;
                    l8.Text = "День рождение: " + (DateTime.Parse(row.Birthday.ToString()).ToLongDateString()).Remove(DateTime.Parse(row.Birthday.ToString()).ToLongDateString().Length - 7);
                    tCell.Controls.Add(l8);
                }
                //--
                tRow.Cells.Add(tCell);
                //--
                //tb.Rows.Add(tRow);
                tb.Rows.Add(tRow);
                Panel2.Controls.Add(tb);
            }
        }

        private void LoadManagedMetaDataInformation()
        {
            TaxonomySession session = new TaxonomySession(SPContext.Current.Site);
            TermStoreCollection termstores = session.TermStores;
            foreach (TermStore store in termstores)
            {
                
                TreeNode node = new TreeNode();
                node.Text = store.Name;
                string StoreName = "АУП";
                if (CustomCheckboxProp)
                    StoreName = "Магазины";

                foreach (Group g in store.Groups)
                {
                    if (g.Name == "Структура Организации")
                    {
                        TreeNode child = new TreeNode();
                        child.Text = g.Name;
                        foreach (TermSet ts in g.TermSets)
                        {
                            if (ts.Name == StoreName) // "АУП" or "Магазины"
                            {
                                TreeNode child2 = new TreeNode();
                                child2.Text = "Структура организации";
                                    
                                GetTermSets2(child2, ts.Terms);
                            }
                        }
                    }
                }
                
            }
        }



        private void GetTermSets2(TreeNode node, TermCollection termCollection)
        {
            foreach (Term ts in termCollection)
            {
                TreeNode child = new TreeNode();
                child.Text = ts.Name;
                child.Collapse();
                node.ChildNodes.Add(child);
              
                //GetTermSets3(child, ts.Terms);
                foreach (Term tss in ts.Terms)
                {
                    TreeNode child2 = new TreeNode();
                    child2.Text = tss.Name;
                    child.ChildNodes.Add(child2);
                }
                
            }
            theTree.Nodes.Add(node);
        }



       
    }
}
